package org.example.route.util;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;

import java.util.List;
import java.awt.Color;

public class ExcelUtils {


    public static int writeTitlesToExcel(XSSFWorkbook wb, Sheet sheet, short height, int rowIndex, List<String> titles) {
        // 设置字体
        Font titleFont = wb.createFont();
        titleFont.setFontName("simsun");    //  字体类型
        titleFont.setBold(true);    //  加粗
        titleFont.setColor(IndexedColors.BLACK.index);  //  字体颜色
        titleFont.setFontHeight(height);

        // 设置表单样式
        XSSFCellStyle titleStyle = wb.createCellStyle();
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        // 创建 CTColor 对象
        CTColor ctFillColor = CTColor.Factory.newInstance();
        ctFillColor.setRgb(new byte[]{(byte) 182, (byte) 184, (byte) 192});
        XSSFColor fillColor = new XSSFColor(ctFillColor, null);
        titleStyle.setFillForegroundColor(fillColor);
        titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        titleStyle.setFont(titleFont);

        // 创建边框颜色的 CTColor 对象
        CTColor ctBorderColor = CTColor.Factory.newInstance();
        ctBorderColor.setRgb(new byte[]{0, 0, 0});
        XSSFColor borderColor = new XSSFColor(ctBorderColor, null);
        setBorder(titleStyle, BorderStyle.THIN, borderColor);

        Row titleRow = sheet.createRow(rowIndex);
        int colIndex = 0;
        for (String field : titles) {
            Cell cell = titleRow.createCell(colIndex);
            cell.setCellValue(field);
            cell.setCellStyle(titleStyle);
            colIndex++;
        }
        rowIndex++;
        return rowIndex;
    }


    private static void setBorder(XSSFCellStyle style, BorderStyle border, XSSFColor color) {
        style.setBorderTop(border);
        style.setBorderLeft(border);
        style.setBorderRight(border);
        style.setBorderBottom(border);
        style.setBorderColor(XSSFCellBorder.BorderSide.TOP, color);
        style.setBorderColor(XSSFCellBorder.BorderSide.LEFT, color);
        style.setBorderColor(XSSFCellBorder.BorderSide.RIGHT, color);
        style.setBorderColor(XSSFCellBorder.BorderSide.BOTTOM, color);
    }

    public static int writeRowsToExcel(XSSFWorkbook wb, Sheet sheet, int rowIndex, List<List<String>> list) {
        Font dataFont = wb.createFont();
        dataFont.setFontName("simsun");
        dataFont.setColor(IndexedColors.BLACK.index);

        XSSFCellStyle dataStyle = wb.createCellStyle();
        dataStyle.setAlignment(HorizontalAlignment.CENTER);
        dataStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        dataStyle.setFont(dataFont);

        // 创建边框颜色的 CTColor 对象
        CTColor ctBorderColor = CTColor.Factory.newInstance();
        ctBorderColor.setRgb(new byte[]{0, 0, 0});
        XSSFColor borderColor = new XSSFColor(ctBorderColor, null);
        setBorder(dataStyle, BorderStyle.THIN, borderColor);

        for (List<String> all : list) {
            Row dataRow = sheet.createRow(rowIndex);
            int colunm = 0;
            for (String s : all) {
                Cell cell = dataRow.createCell(colunm);
                cell.setCellValue(s);
                cell.setCellStyle(dataStyle);
                colunm++;
            }
            rowIndex++;
        }
        return rowIndex;
    }

}
